package cn.hwh.springframework.beans.factory.support;

import cn.hwh.springframework.beans.factory.BeanFactory;
import cn.hwh.springframework.beans.factory.config.BeanDefinition;

/**
 * @author wenhao hu
 * @title: AbstractBeanFactory
 * @projectName mini-spring
 * @description: TODO
 * @date 2022/1/2411:38
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
    @Override
    public Object getBean(String name) {
        return doGetBean(name, null);
    }

    @Override
    public Object getBean(String name, Object... args) {
        return doGetBean(name, args);
    }

    protected <T> T doGetBean(final String name, final Object[] args) {
        Object bean = getSingleton(name);
        if (bean != null) {
            return (T) bean;
        }

        BeanDefinition beanDefinition = getBeanDefinition(name);
        return (T) createBean(name, beanDefinition, args);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName);
    protected abstract Object createBean(String beanName,BeanDefinition beanDefinition,Object ...args);
}
